//
//  ButtonUI.swift
//  uiKit-demo
//
//  Created by 陈澎 on 2024/12/20.
//
import UIKit
import Foundation
public class MyButtonUI: UIViewController{
    @objc func touchBegin() {
        print("用户点击了吗")
    }
    func btnTest(_ self: AnyObject, target: Any) {
        print("渲染按钮")

        let buttonOne = UIButton(type: UIButton.ButtonType.system)
        buttonOne.frame = CGRect(x:20, y: 500, width: 100, height: 30)
        buttonOne.backgroundColor = UIColor.purple
        buttonOne.setTitle("确定", for: .normal)
        buttonOne.setTitle("不可点击", for: .disabled)
        buttonOne.setTitleColor(UIColor.white, for: .normal)
        buttonOne.setImage(UIImage(named: "image"), for: .normal)
        // 设置背景图片
        buttonOne.setBackgroundImage(UIImage(named: "bgImg"), for: .normal)

        // 事件触发
        buttonOne.addTarget(target, action: #selector(MyButtonUI.touchBegin), for: UIControl.Event.touchUpInside)

        self.addSubview(buttonOne)
        
        let tipBtn = UIButton(type: UIButton.ButtonType.detailDisclosure)
        tipBtn.frame = CGRect(x: 140, y: 500, width: 100, height: 30)
        tipBtn.backgroundColor =  UIColor.red
        tipBtn.setTitle("详情", for: .normal)
        tipBtn.setTitleColor(UIColor.white, for: .normal)
        self.addSubview(tipBtn)
        
        let customBtn = UIButton(type: .custom)
        customBtn.frame = CGRect(x: 260, y: 500, width: 100, height: 30)
        // 设置内容图片 只有自定义的情况下才会生效
        customBtn.setBackgroundImage(UIImage(named: "bgImg"), for: .normal)
        customBtn.setTitle("自定义", for: .normal)
        let cryImg = UIImage(named: "cryImg")
        // 设置图片大小处理
        let resizeCryImg = UIGraphicsImageRenderer(size: CGSize(width: 20, height: 20)).image { _ in
            cryImg?.draw(in: CGRect(origin: .zero, size: CGSize(width: 20, height: 20)))
        }
        customBtn.setImage(resizeCryImg,  for: .normal)
        // 修改内容图片与标题之间的位置
        customBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 20)
        customBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 0)
        self.addSubview(customBtn)
    }
}


